﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BussinesLogic;


namespace FoliaturaDigital
{
    public partial class frmCrearExpediente : Form
    {

        Frm_Expediente _owner;
        Foliatura.Controller controlador;
        bool isEdit = true;
        bool isNuevo = false;

        public frmCrearExpediente(Frm_Expediente owner)
        {
            _owner = owner;
            this.controlador = Foliatura.Controller.Instance();
            InitializeComponent();
        }
        private void frmCrearExpediente_FormClosing(object sender, FormClosingEventArgs e)
        {
            _owner.DataBind();
        }

        
        private void frmCrearExpediente_Load(object sender, EventArgs e)
        {
            //this.controlador.expediente = Expediente.Portada.getPortadabyExpediente(this.controlador.expediente.NoExpediente);
            isEdit=isNuevo = controlador.expediente.id <= 0;
            fillData();
            enableAll(isEdit);
        }


        private void fillData()
        {
            if (controlador.expediente != null)
            {
                this.txtNoExpediente.Text = this.controlador.expediente.NoExpediente;
                this.txtDescripcion.Text = this.controlador.expediente.Descripcion;                
            }
            this.lblUser.Text = "Usuario: "+this.controlador.user.Nombre1 + " " + this.controlador.user.Apellido1;

        }
        private void enableAll(bool enabled)
        {

            this.txtNoExpediente.Enabled = enabled;
            this.txtDescripcion.Enabled = enabled;           
        }

        private void bindData()
        {
            this.controlador.expediente.NoExpediente = this.txtNoExpediente.Text;
            this.controlador.expediente.Descripcion = this.txtDescripcion.Text;
            this.controlador.expediente.ModifiedBy = this.controlador.user.Username;
            this.controlador.expediente.CreatedBy = this.controlador.user.Username;
            this.controlador.expediente.ModifyDate = DateTime.Now;
            this.controlador.expediente.StartDate = DateTime.Now;

        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            bindData();
            if (this.isNuevo)
            {
                bindData();
                try
                {
                    this.controlador.expediente.Create();
                    fillData();
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                
            }    
        
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            this.isEdit = !this.isEdit;
            if (this.isEdit)
            {
                this.btnEditar.ButtonText = "Cancelar";
                this.btnGuardar.Enabled = true;

            }
            else
            {
                this.btnEditar.ButtonText = "Editar";
                this.btnGuardar.Enabled = false;

            }
            enableAll(this.isEdit);
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

     

        

    }
}
